package org.example.controller;

import cn.lingyangwl.framework.core.response.Resp;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author shenguangyang
 */
@RestController
public class IndexController {
    @Resource
    private WxMpService wxMpService;
    @Resource
    private WxMpConfigStorage wxMpConfigStorage;

    @GetMapping("test")
    public void testAutowire(){
        System.out.println(wxMpService);
        System.out.println(wxMpConfigStorage);
    }

    @GetMapping("/getAccessToken")
    public Resp<?> getAccessToken() throws WxErrorException {
        return Resp.ok(wxMpService.getAccessToken(false));
    }

    /**
     * 接入微信公众号回调
     * <a href="https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index">地址</a>
     * 配置接口后点击提交, 微信验证地址是否合法会向配置的地址发送一个get请求
     */
    @GetMapping("/")
    public String configAccess(String signature, String timestamp, String nonce, String echostr) {
        // 校验签名
        if (wxMpService.checkSignature(timestamp, nonce, signature)){
            // 校验成功原样返回 echostr
            return echostr;
        }
        // 校验失败
        return "";
    }

}
